package gepes.dao;

import gepes.exepcion.DAOExcepcion;
import gepes.modelo.Estudiante;
import gepes.util.Conexion;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class EstudianteDAO  extends UsuarioDAO{
	public Estudiante insertar(Estudiante vo, String ruta) throws DAOExcepcion {

		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {

			con = Conexion.ConectarDB();
			String query = "insert into usuario(codtipousuario,apepat,apemat,nombres,sexo,"
					+ "codtipodocumento,nrodocumento,mail,nrocelular,password,perfil,foto,"
					+ "estado,fechareg,usuarioreg) "
					+ " values(2,?,?,?,?,"
					+ "?,?,?,?,md5(?),?,?," + "1,now(),?)";

			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getApePat());
			stmt.setString(2, vo.getApeMat());
			stmt.setString(3, vo.getNombres());
			stmt.setString(4, vo.getSexo());
			stmt.setInt(5, vo.getTipoDocumento().getCodTipoDocumento());
			stmt.setString(6, vo.getNroDocumento());
			stmt.setString(7, vo.getMail());
			stmt.setString(8, vo.getNroCelular());
			stmt.setString(9, vo.getPassword());
			stmt.setString(10, vo.getPerfil());

			if (!ruta.equals("")) {
				//
				File file = new File(ruta);
				FileInputStream imagen = new FileInputStream(file);

				FileInputStream fis = new FileInputStream(file);
				stmt.setBinaryStream(11, fis, (int) file.length());
				fis.close();
				//
			} else {
				;
				stmt.setBinaryStream(11, null);
			}

			stmt.setString(12, vo.getUsuarioReg());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("Error insertar");
			}

			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			vo.setCodUsuario(id);

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public Estudiante editar(Estudiante vo) throws DAOExcepcion {

		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {

			con = Conexion.ConectarDB();
			String query = "update usuario set apepat=?,apemat=?,nombres=?,sexo=?,"
					+ "codtipodocumento=?,nrodocumento=?,nrocelular=?,perfil=?"
					+ " where codusuario=?";

			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getApePat());
			stmt.setString(2, vo.getApeMat());
			stmt.setString(3, vo.getNombres());
			stmt.setString(4, vo.getSexo());
			stmt.setInt(5, vo.getTipoDocumento().getCodTipoDocumento());
			stmt.setString(6, vo.getNroDocumento());
			stmt.setString(7, vo.getNroCelular());
			stmt.setString(8, vo.getPerfil());
			stmt.setInt(9, vo.getCodUsuario());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("Error editar");
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

}
